import { Apilogin, ApigetUsePatient } from "../../api/index"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  goDoctor() {
    wx.navigateTo({
      url: '/packageA/doctorLogin/index',
    })
  },

  onLogin() {
    this.setData({
      loading: true
    })
    wx.login({
      success: (res) => {
        Apilogin({js_code: res.code}).then(res => {
          wx.setStorageSync('token', res.data.token)
          wx.setStorageSync('loginType', "patient")
          return ApigetUsePatient()
        }).then(res => {
          app.setUserInfo(res.data)
          app.setWebScoket()
          this.goPage()
        }).finally(_=>{
          this.setData({
            loading: false
          })
        })
      },
      fail: (err) => {
        wx.showToast({
          title: '获取code失败',
          icon: "none"
        })
      }
    })
  },

  goPage(){
    let fromUrl = wx.getStorageSync('fromUrl')
    if (fromUrl) {
      wx.reLaunch({
        url: fromUrl,
        success: () => {
          wx.removeStorage({
            key: 'fromUrl',
          })
        }
      });
    } else {
      wx.reLaunch({
        url: "/pages/index/index",
      });
    }
  },
})